// pages/login/login.js
const login = require("../../models/login");
Page({
  data: {
    disabled:true,
    username:'',
    password:""
  },
  onLoad: async function (options) {

  },
  // 验证账号密码
  async submit(e){
    var data = await login(e.detail.value.username,e.detail.value.password);
    if(data.data.res){
      wx.showToast({
        title: data.data.msg,
        icon: 'loading',
        duration: 2000
      })
      wx.setStorageSync('id', data.data.res[0]._id);
      wx.navigateBack({
        delta: 1, // 回退前 delta(默认为1) 页面
      })
    }else{
      console.log("error");
      wx.showToast({
        title: data.data.msg,
        icon: 'none',
        duration: 2000
      })
    }
  },

  //当输入账号密码时候才启用登录按钮 
  onChangeUsername(e){
    this.setData({
      username:e.detail
    })
    if(this.data.username.length>0&&this.data.password.length>0){
      this.setData({
        disabled:false
      })
    }else{
      this.setData({
        disabled:true
      })
    }
  },
  onChangePassword(e){
    this.setData({
      password:e.detail
    })
    if(this.data.username.length>0&&this.data.password.length>=4){
      this.setData({
        disabled:false
      })
    }else{
      this.setData({
        disabled:true
      })
    }
  },
  clickRegister(){
    wx.navigateTo({
      url: '../register/register',
    })
  }
})